/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sansecy.app;

import android.annotation.SuppressLint;
import android.arch.core.executor.ArchTaskExecutor;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Global executor pools for the whole application.
 * <p>
 * Created by jon on 2018/5/14.
 */
@SuppressLint("RestrictedApi")
public final class SPExecutor {

    private volatile Handler mMainHandler;

    private final Executor mNetworkIO;

    private SPExecutor() {
        mMainHandler = new Handler(Looper.getMainLooper());
        mNetworkIO = new ThreadPoolExecutor(5, 15,
                20L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    @NonNull
    public static SPExecutor get() {
        return SPExecutorsHolder.instance;
    }

    /**
     * run on disk io
     *
     * @param runnable runnable
     */
    public void executeOnDiskIO(@NonNull Runnable runnable) {
        ArchTaskExecutor.getInstance().executeOnDiskIO(runnable);
    }

    /**
     * run on network io
     *
     * @param runnable runnable
     */
    public void executeOnNetworkIO(@NonNull Runnable runnable) {
        mNetworkIO.execute(runnable);
    }

    /**
     * run on main thread
     *
     * @param runnable runnable
     */
    public void runOnUiThread(@NonNull Runnable runnable) {
        mMainHandler.post(runnable);
    }

    /**
     * run on main thread
     *
     * @param runnable runnable
     */
    public void runOnUiThreadDelayed(@NonNull Runnable runnable, long delayMillis) {
        mMainHandler.postDelayed(runnable, delayMillis);
    }

    public void removeUiThreadCallbacks(@NonNull Runnable runnable) {
        mMainHandler.removeCallbacks(runnable);
    }

    /**
     * get main thread executor
     *
     * @return main thread executor
     */
    @NonNull
    public Handler getUiThreadHandler() {
        return mMainHandler;
    }

    /**
     * get disk thread executor
     *
     * @return disk thread executor
     */
    @NonNull
    public Executor getDiskIOThreadExecutor() {
        return ArchTaskExecutor.getIOThreadExecutor();
    }

    /**
     * get network thread executor
     *
     * @return network thread executor
     */
    @NonNull
    public Executor getNetworkIOThreadExecutor() {
        return mNetworkIO;
    }

    /**
     * get queue thread executor
     *
     * @return queue thread executor
     */
    @NonNull
    public ExecutorService getQueueIOThreadExecutor() {
        return Executors.newSingleThreadExecutor();
    }

    /**
     * is main thread
     *
     * @return true:yes,false:no.
     */
    public boolean isMainThread() {
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }

    private static class SPExecutorsHolder {
        private static SPExecutor instance = new SPExecutor();
    }
}
